#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "StudentMain.h"

void AddStudent(node* head)
{
	stu AddInfo;
	printf("\n欢迎来到添加模式!\n");
	printf("\n请根据提示输入您想添加的人员信息!\n");
	printf("\n学生学号:>");
	scanf("%d", &AddInfo.id);
	printf("学生年龄:>");
	scanf("%d", &AddInfo.age);
        printf("C语言成绩:>");
	scanf("%d",&AddInfo.program);
	while(AddInfo.program < 0)
	{
		//判断成绩是否小于0
		printf("\n输入成绩有！请重新输入！\n");
		scanf("%d", &AddInfo.program);
	}
        printf("学生姓名:>");
	scanf("%s", AddInfo.name);
        printf("学生性别:>");
	scanf("%s", AddInfo.sex);
	//调用头插法
	InsertHead(head, &AddInfo, sizeof(stu));
	printf("\n添加成功！\n");
}

void InsertHead(node* head, stu* data, int size)
{
	node* pnew = (node*)malloc(sizeof(node));//为新节点申请内存
	if (pnew == NULL)
	{
		printf("\nmalloc new fail!\n");
		exit(-1);
	}
	
	//给新节点赋值
	pnew->prve = NULL;
	pnew->next = NULL;
	pnew->data = malloc(sizeof(size));
	memcpy(pnew->data, data, size);
	
	if(head->next == head)
	{
		pnew->prve = head;
		pnew->next = head;
		head->next = pnew;
		head->prve = pnew;
	}
	else
	{	
		pnew->prve = head;
		pnew->next = head->next;
		head->next = pnew;
		head->next->prve = pnew;
	}
}
